﻿@model WXY.Course.Model.ResourceInfo
@{
    var moduleList = ViewData["ModuleList"] as IList<WXY.Course.Model.ModuleInfo>;
}

<div class="m_rtop">
    <h3>
        <label>
            内容管理
        </label>
        <span>></span>@(Model == null ? "添加" : "修改")文章
    </h3>
</div>
<div class="m_box">
    <div class="c_finfo">
        <p>
            <label>
                标题：
            </label>
            <input type="text" class="input_w" id="title" value="@Model.Title" maxlength="100">
        </p>
        <p>
            <label>
                分类：
            </label>
            <select id="moduleId">
                <option value="-1">全部</option>
                @{
                    if (moduleList != null)
                    {
                        foreach (var item in moduleList)
                        {
                            <option value="@item.Code" @if (Model.ModuleInfo != null && Model.ModuleInfo.Code == item.Code) { <text> selected="selected" </text>  }>@item.Name</option>
                        }
                    }
                }
            </select>
        </p>
        <p>
            <label>
                内容：
            </label>
        </p>
        <div class="txt_box01">
            <textarea id="content">@Model.Content</textarea>
        </div>
        <p class="m_btn">
            <input type="hidden" id="id" value="@Model.Code" />
            <input type="button" id="btnSubmit" class="btn_bg1" value="提交">
            <input type="button" id="btnReturn" onclick="history.go(-1)" class="btn_bg1" value="返回">
        </p>
    </div>
</div>
<script src="~/Content/admin/Scripts/ckeditor/ckeditor.js"></script>
<script type="text/javascript">
    $(function () {
        var normalDialog = function (msg) {
            art.dialog({
                drag: true,
                lock: true,
                width: 310,
                height: 50,
                title: '提示',
                content: msg,
                ok: function () {
                    this.close();
                }
            });
        };
        function getLength(value) {
            var replaceVal = value.replace(/[^\u0000-\u00ff]/g, "11");
            return replaceVal.length;
        };
        var valid = function (data) {
            if (data.title.length == 0) {
                normalDialog('请输入标题');
                return false;
            } else if (getLength(data.title) > 50) {
                normalDialog('标题名称长度应不大于50个字符，一个汉字等于两个字符');
                return false;
            }
            if (!data.moduleId || data.moduleId <= 0) {
                normalDialog('请选择分类');
                return false;
            }
            if (!data.content || data.content == 0) {
                normalDialog('请输入内容');
                return false;
            }
            return true;
        };

        var generatePostData = function () {
            return {
                id: $("#id").val(),
                title: $("#title").val(),
                moduleId: $("#moduleId").val(),
                content: CKEDITOR.instances.content.getData()
            }
        }

        $("#btnSubmit").on("click", function () {
            var postData = generatePostData();
            if (valid(postData)) {
                $.ajax({
                    type: "post",
                    url: "@Url.Action("Edit", "Resource")",
                    data: postData,
                    success: function (data) {
                        if (data.IsSuccess) {
                            art.dialog({
                                drag: true,
                                lock: true,
                                width: 310,
                                height: 50,
                                title: '提示',
                                content: "提交成功",
                                ok: function () {
                                    location.href = "@Url.Action("List","Resource")";
                                }
                            });
                            return;
                        }
                        normalDialog(data.Message);
                    }
                });
            }
        });
        CKEDITOR.replace('content', {
            toolbar: [
                ['Source', '-', 'NewPage'],
                ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo'],
                ['Find', 'Replace', 'Scayt'],
                ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'],
                '/',
                ['Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat'],
                ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote', 'CreateDiv', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-', 'BidiLtr', 'BidiRtl', 'Language'],
                ['Link', 'Unlink', 'Anchor'],
                ['Image', 'OfficeIframe', 'Flash', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak'],
                '/',
                ['Styles', 'Format', 'Font', 'FontSize'],
                ['TextColor', 'BGColor'],
                ['Maximize', 'ShowBlocks']

            ],
            height: 500,
            width: 800
        });
    });
</script>
